<template lang="pug">
  Card
    Button(type='primary',v-if="$route.meta['/role/add']",style='margin-bottom:20px',@click='$router.push({name:"role_setting",query:{status:"add"}})') 权限设置
    show-table(ref='tab',:total='total',:current='current',:columns='columns',:data='roleData',@handleChangePage='handleChangePage')
</template>

<script>
import { asyncRole } from '../api'
import showTable from '@/components/show-table/show-table'

export default {
  name: 'RoleManage',
  components: { showTable },
  data() {
    return {
      total: 0,
      current: 1,
      roleData: [],
      columns: [
        {
          title: 'ID',
          width: 70,
          align: 'center',
          key: 'id'
        },
        {
          title: '角色名称',
          render: (h, params) => {
            if (this.$route.meta['/role/update']) {
              return h(
                'router-link',
                { attrs: { to: { name: 'role_setting', query: { status: 'modify', name: params.row.roleName, roleId: params.row.id } } } },
                params.row.roleName
              )
            } else {
              return h('span', params.row.roleName)
            }
          }
        },
        {
          title: '创建时间',
          key: 'createDate'
        },
        {
          title: '创建人',
          key: 'creator'
        }
      ]
    }
  },
  mounted() {
    this.handleRole()
    this.handleRoleListData()
  },
  methods: {
    handleChangePage(pageSub) {
      this.handleUpdateTableData(pageSub)
    },
    handleUpdateTableData(current = 1) {
      this.current = current
      this.handleRoleListData()
    },
    handleRoleListData: async function() {
      try {
        this.total = 0
        this.roleData = []
        this.$refs['tab'].loading = true
        const res = await asyncRole.list({ data: { pageNo: this.current, pageSize: this.$refs['tab'].pageSize } })
        this.roleData = res.data.data.data
        this.total = res.data.data.totalCount
        this.$refs['tab'].loading = false
      } catch (error) {
        this.$refs['tab'].loading = false
      }
    },
    handleRole: async function() {
      try {
        const roleData = await asyncRole.getRoleRight({ data: { roleId: '' } })
        sessionStorage.setItem('roleData', JSON.stringify(roleData.data.data.roleRightVoList))
      } catch (error) {}
    }
  }
}
</script>
